from scrapy import Spider, Request
from scrapy_playwright.page import PageMethod

class HackerNewsLoginSpider(Spider):
    name = "hn_login"
    start_urls = ["https://gitee.com/login"]
    site_name = "gitee.com"
    
    
    def start_requests(self):
        yield Request(
            url="https://gitee.com/login",
            meta={
                "playwright": True,
                "playwright_include_page": True,
                "playwright_page_methods": [
                    PageMethod("fill", 'input#user_login', "4384213@qq.com"),  # 替换为你的用户名
                    PageMethod("fill", 'input#user_password', "JHQ@123"),  # 替换为你的密码
                    PageMethod("click", 'input[type="submit"]'),  # 点击登录按钮
                    PageMethod("wait_for_load_state", "networkidle"),
                ],
            },
            callback=self.after_login,
        )

    def after_login(self, response):
        # 登录成功后，可以继续抓取其他页面 fill("4384213@qq.com")
        self.logger.info("Login successful. Proceeding to scrape data.")
        # 示例：抓取登录后的页面内容
        yield {
            "title": response.css("title::text").get(),
        }